package com.collaborativelearning;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.collaborativelearning.dummy.DummyContent;
import com.collaborativelearning.dummy.Management;

/**
 * A fragment representing a single Unit detail screen. This fragment is either
 * contained in a {@link UnitListActivity} in two-pane mode (on tablets) or a
 * {@link UnitDetailActivity} on handsets.
 */
public class UnitDetailFragment extends Fragment {
	/**
	 * The fragment argument representing the item ID that this fragment
	 * represents.
	 */
	public static final String ARG_ITEM_ID = "item_id";

	/**
	 * The dummy content this fragment is presenting.
	 */
	private DummyContent.DummyItem mItem;

	/**
	 * Mandatory empty constructor for the fragment manager to instantiate the
	 * fragment (e.g. upon screen orientation changes).
	 */
	public UnitDetailFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments().containsKey(ARG_ITEM_ID)) {
			// Load the dummy content specified by the fragment
			// arguments. In a real-world scenario, use a Loader
			// to load content from a content provider.
			mItem = DummyContent.ITEM_MAP.get(getArguments().getString(
					ARG_ITEM_ID));
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_unit_detail,
				container, false);

		// Show the dummy content as text in a TextView.
		if (mItem != null) {
			((TextView) rootView.findViewById(R.id.textview1))
					.setText(mItem.content);
			Button btn_goto_home = (Button) rootView
					.findViewById(R.id.btn_goto_home);
			btn_goto_home.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if (Management.INSTANCE.isTeacher()) {
						Intent i = new Intent(getActivity(),
								TeacherHomePage.class);
						startActivity(i);
						return;
					}
					if (Management.INSTANCE.isStudent()) {
						Intent i = new Intent(getActivity(),
								StudentHomePage.class);
						startActivity(i);
					}
				}
			});
			
			final Spinner notes_spinner = (Spinner) rootView
					.findViewById(R.id.notes_spinner);
			//notes_spinner.
			
			notes_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			    @Override
			    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, final int position, long id) {
			        if (position == 0) {
						return;
					}
			    	
			        AlertDialog.Builder editalert = new AlertDialog.Builder(getActivity());
					editalert.setTitle("شاهد أو عدل الملاحظة!");
					final EditText input = new EditText(getActivity());
					LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
					        LinearLayout.LayoutParams.MATCH_PARENT,
					        LinearLayout.LayoutParams.MATCH_PARENT);
					input.setLayoutParams(lp);
					input.setGravity(Gravity.RIGHT);
					input.setText(mItem.notes.get(position));
					editalert.setView(input);

					editalert.setPositiveButton("حفظ", new DialogInterface.OnClickListener() {
					    public void onClick(DialogInterface dialog, int whichButton) {
					    	String note = input.getText().toString();
					    	mItem.notes.set(position, note);
					    }
					});
					
					editalert.setNegativeButton("إلغاء", new DialogInterface.OnClickListener() {
					    public void onClick(DialogInterface dialog, int whichButton) {
					    	Toast.makeText(getActivity(), "الملاحظة لم تعدل!", Toast.LENGTH_LONG).show();
					    }
					});
					editalert.show();
					notes_spinner.setSelection(0);
			    }

			    @Override
			    public void onNothingSelected(AdapterView<?> parentView) {
			        // your code here
			    }

			});
			
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, mItem.notes);
			notes_spinner.setAdapter(adapter);
			
			Button btn_add_notes = (Button) rootView
					.findViewById(R.id.btn_add_notes);
			btn_add_notes.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					
					AlertDialog.Builder editalert = new AlertDialog.Builder(getActivity());
					editalert.setTitle("أضف ملاحظتك!");
					final EditText input = new EditText(getActivity());
					LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
					        LinearLayout.LayoutParams.MATCH_PARENT,
					        LinearLayout.LayoutParams.MATCH_PARENT);
					input.setLayoutParams(lp);
					input.setGravity(Gravity.RIGHT);
					editalert.setView(input);

					editalert.setPositiveButton("حفظ", new DialogInterface.OnClickListener() {
					    public void onClick(DialogInterface dialog, int whichButton) {
					    	String note = input.getText().toString();
					    	mItem.notes.add(note);
					    }
					});
					
					editalert.setNegativeButton("إلغاء", new DialogInterface.OnClickListener() {
					    public void onClick(DialogInterface dialog, int whichButton) {
					    	Toast.makeText(getActivity(), "الملاحظة لم تسجل!", Toast.LENGTH_LONG).show();
					    }
					});
					editalert.show();
				}
			});
			
		}

		return rootView;
	}
}